//
//  FilterController.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 12/24/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "FilterController.h"
#import "CheckItem.h"
#import "CheckBoxCell.h"
#import "CheckboxHeaderView.h"


@interface FilterController ()

@end

@implementation FilterController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _data = [[NSMutableArray alloc] init];
    
    CheckItem *parent = [[CheckItem alloc] init];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [_data addObject:parent];
    
    parent = [[CheckItem alloc] init];
    [parent.children addObject:[[CheckItem alloc] init]];
    [_data addObject:parent];
    
    parent = [[CheckItem alloc] init];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [_data addObject:parent];
    
    parent = [[CheckItem alloc] init];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [_data addObject:parent];
    
    parent = [[CheckItem alloc] init];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [parent.children addObject:[[CheckItem alloc] init]];
    [_data addObject:parent];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)newCheckItemWithindex:(int)index
{
    CheckItem *item = [[CheckItem alloc] init];
    item.index = index;
}

#pragma mark - Delegate UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _data.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    CheckboxHeaderView *header = [[CheckboxHeaderView alloc] init];
    CheckItem *item = [_data objectAtIndex:section];
    header.checkBox.textLabel.text = [NSString stringWithFormat:@"header %d",section];
    header.checkBox.checked = item.checked;
    header.checkBox.tag = section;
    [header.checkBox addTarget:self action:@selector(headerCheckedChanged:) forControlEvents:UIControlEventValueChanged];
    return header;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    CheckItem *item = [_data objectAtIndex:section];
    return item.children.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString *cellIdentifier = @"checkCell";
    
    CheckBoxCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell)
    {
        cell = [[CheckBoxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    CheckItem *headerItem = [_data objectAtIndex:indexPath.section];
    CheckItem *item = [headerItem.children objectAtIndex:indexPath.row];
    
    cell.checkBox.checked = item.checked;
    cell.checkBox.indexPath = indexPath;
    [cell.checkBox addTarget:self action:@selector(cellCheckedChanged:) forControlEvents:UIControlEventValueChanged];
    cell.checkBox.textLabel.text = [NSString stringWithFormat:@"cell %d",indexPath.row];
    
    return cell;
}

- (void)headerCheckedChanged:(CTCheckbox*)checkbox
{
    CheckItem *headerItem = [_data objectAtIndex:checkbox.tag];
    headerItem.checked = checkbox.checked;
    
    for (CheckItem *child in headerItem.children)
    {
        child.checked = checkbox.checked;
    }
    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:checkbox.tag] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)cellCheckedChanged:(CTCheckbox*)checkbox
{
    CheckItem *headerItem = [_data objectAtIndex:checkbox.indexPath.section];
    CheckItem *item = [headerItem.children objectAtIndex:checkbox.indexPath.row];
    item.checked = checkbox.checked;
    if (checkbox.checked == NO)
    {
        headerItem.checked = NO;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:checkbox.indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
    }
    else
    {
        BOOL isAllChecked = YES;
        for (CheckItem *child in headerItem.children)
        {
            if(child.checked == NO)
            {
                isAllChecked = NO;
                break;
            }
        }
        
        if (isAllChecked)
        {
            headerItem.checked = YES;
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:checkbox.indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
        }
        else
        {
            [self.tableView reloadRowsAtIndexPaths:@[checkbox.indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }
    }
}

@end
